package com.example.templateapp.ui.dropdown

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnticipateInterpolator
import android.view.animation.RotateAnimation
import android.widget.PopupWindow
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.example.templateapp.R
import com.example.templateapp.databinding.ActivityDropDownBinding
import com.example.templateapp.ui.adapter.base.SimpleDelegateAdapter
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder

class DropDownActivity : AppCompatActivity() {

    private lateinit var binding: ActivityDropDownBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_drop_down)

        binding.dropDown.setOnClickListener {
            startArrowAnim(it)
            val popupWindow = getPopupWindow(this@DropDownActivity, arrayOf("1", "2"))
            if (popupWindow.isShowing) {
                popupWindow.dismiss()
            } else {
                popupWindow.showAsDropDown(it, 0, 10, Gravity.BOTTOM)
            }
        }
    }

    private fun getPopupWindow(context: Context, data: Array<String>): PopupWindow {
        return PopupWindow(this).apply {
            contentView = LayoutInflater.from(context)
                .inflate(R.layout.layout_custom_down_menu, null)
            val rv = contentView.findViewById<RecyclerView>(R.id.rv)
            initRv(rv, data) { s ->
                binding.dropDown.text = s
                dismiss()
            }

            setOnDismissListener { startArrowAnim(binding.dropDown) }
            width = ViewGroup.LayoutParams.MATCH_PARENT
            setBackgroundDrawable(ColorDrawable(Color.WHITE))
            isFocusable = true //否则isShowing总是返回false
            isOutsideTouchable = true
        }
    }

    private fun initRv(rv: RecyclerView, data: Array<String>, l: (s: String) -> Unit) {
        rv.adapter = object : SimpleDelegateAdapter<String>(
            R.layout.adapter_item_tv_match, LinearLayoutHelper(),
            data
        ) {
            override fun bindData(holder: RecyclerViewHolder, position: Int, item: String) {
                holder.text(R.id.tv_item, item)
                holder.itemView.setOnClickListener {
                    l(item)
                }
            }
        }
    }

    private fun startArrowAnim(it: View) {
        val animation = if (it.tag == "up") {
            it.tag = "down"
            rotateAnimation(180f, 0f)
        } else {
            it.tag = "up"
            rotateAnimation(0f, 180f)
        }
        binding.downArrow.startAnimation(animation)
    }

    private fun rotateAnimation(from: Float, to: Float): RotateAnimation {
        val animation = RotateAnimation(
            from, to,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f
        )
        animation.interpolator = AnticipateInterpolator()
        animation.duration = 300
        animation.fillAfter = true
        return animation
    }
}